/**
 * an abstactloader that is common for all loaders in the ch.sfug.net.loader package
 */

package ch.sfug.net.loader {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.net.URLVariables;	

	public class AbstractLoader extends EventDispatcher {

		private var req:URLRequest;
		private var _run:Boolean;

		public var disableCache:Boolean = false;

		/**
		 * creates a loader that stores the url to load from
		 */
		public function AbstractLoader( req:URLRequest = null ) {
			this.request = req;
		}

		
		/**
		 * sets the url to load from
		 */
		public function set request( r:URLRequest ):void {
			if( r != null ) req = r;
		}

		
		/**
		 * returns the url
		 */
		public function get request(  ):URLRequest {
			if( disableCache ) {
				var data:URLVariables = ( req.data != null ) ? URLVariables(req.data) : new URLVariables();
				data.random = Math.round(Math.random() * 10000);
				req.data = data;
			}
			return this.req;
		}

		
		/**
		 * returns true if the loader is loading
		 */
		public function get running(  ):Boolean {
			return _run;
		}

		
		/**
		 * ABSTRACT function that starts the load progress
		 *
		 */
		public function load( url:URLRequest = null ):void {
            // abstract
		}

		
		/**
		 * stops the load
		 */
		public function stop(  ):void {
            // abstract
		}

		
		/**
		 * should be called when the load starts
		 */
		protected function onLoadStarts( e:Event = null ):void {
			_run = true;
			if( e != null ) dispatchEvent(e);
		}

		
		/**
		 * should be called when the load is finished
		 */
		protected function onLoadEnds( e:Event = null ):void {
			_run = false;
			if( e != null ) dispatchEvent(e);
		}
	}
}



